//
//  ViewController.m
//  Image
//
//  Created by 解晓东 on 15/11/15.
//  Copyright (c) 2015年 sasa. All rights reserved.
//

#import "ViewController.h"

typedef enum {
    kMoveDirTop = 10,
    kMoveDirLeft,
    kMoveDirRight,
    kMoveDirBottom
} kMoveDir;

typedef enum {
    kZoomSmall = 1,
    kZoomBig
} kZoom;

#define MOVECOUNT 20.0f
#define ZOOMCOUNT 20.0f

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *iconImage;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - 控制图片移动的监听事件
- (IBAction)move:(UIButton *)sender {
    // 取出frame
    // CGRect frame = self.iconImage.frame;
    /*
     优化后的代码
     */
    CGFloat dx = 0;
    CGFloat dy = 0;
    if (sender.tag == kMoveDirTop || sender.tag == kMoveDirBottom) {
        dy = (sender.tag == kMoveDirTop) ? -MOVECOUNT : MOVECOUNT;
    }
    if (sender.tag == kMoveDirLeft || sender.tag == kMoveDirRight) {
        dx = (sender.tag == kMoveDirLeft) ? -MOVECOUNT : MOVECOUNT;
    }
    self.iconImage.transform = CGAffineTransformTranslate(self.iconImage.transform, dx, dy);
    /*
    // 根据tag，判断移动方向
    switch (sender.tag) {
        case kMoveDirTop:
            frame.origin.y -= MOVECOUNT;
            break;
        case kMoveDirLeft:
            frame.origin.x -= MOVECOUNT;
            break;
        case kMoveDirRight:
            frame.origin.x += MOVECOUNT;
            break;
        case kMoveDirBottom:
            frame.origin.y += MOVECOUNT;
            break;
            
        default:
            break;
    }
     
    // 重新为对象的结构体属性赋值
    self.iconImage.frame = frame;
     */
}

#pragma mark - 控制图片缩放的监听事件
- (IBAction)zoom:(UIButton *)sender {
    /*
    CGRect bounds = self.iconImage.bounds;
    switch (sender.tag) {
        case kZoomSmall:
            bounds.size.width -= ZOOMCOUNT;
            bounds.size.height -= ZOOMCOUNT;
            break;
        case kZoomBig:
            bounds.size.width += ZOOMCOUNT;
            bounds.size.height += ZOOMCOUNT;
            break;
            
        default:
            break;
    }
    // 添加首尾式动画
    [UIView beginAnimations:nil context:nil];
    // 设置动画持续时间为1s
    [UIView setAnimationDuration:1.0];
    
    // 动画内容
    self.iconImage.bounds = bounds;
    
    // 提交动画
    [UIView commitAnimations];
     */
    /*
     代码优化
     */
    CGFloat scale = 1.0f;
    if (sender.tag == kZoomSmall) {
        scale = 0.8;
    } else if (sender.tag == kZoomBig) {
        scale = 1.2;
    }
    self.iconImage.transform = CGAffineTransformScale(self.iconImage.transform, scale, scale);
}

#pragma mark - 控制图片旋转的监听事件
- (IBAction)rotate:(UIButton *)sender {
    // 在OC开发中，关于角度统一用弧度制，逆时针是负值
    NSLog(@"%d", sender.tag);
    CGFloat angle = -M_PI_4;
    if (sender.tag == 1) {
       angle = -M_PI_4;
    } else {
        angle = M_PI_4;
    }
    
    self.iconImage.transform = CGAffineTransformRotate(self.iconImage.transform, angle);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
